﻿Imports System.Data.OleDb
Public Class frmRegMedidas

    Private Sub frmRegMedidas_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Preparartabla()


    End Sub
    Sub Preparartabla()
        Dim i As Integer
        Dim data As New DataSet
        Dim comando As New OleDbCommand
        Dim adaptador As New OleDbDataAdapter
        Dim fila As DataRow

        Grid1.Rows.Clear()

        comando.CommandText = "Select nombre from maestro_medidas order by nombre"
        comando.Connection = Conexion
        adaptador.SelectCommand = comando
        adaptador.Fill(data, "tabla")

        i = 0
        For Each fila In data.Tables("tabla").Rows
            Grid1.Rows.Add()
            Grid1.Rows(i).Cells(0).Value = fila.Item("nombre")
            Grid1.Rows(i).Cells(1).Value = 0
            i = i + 1
        Next

       

    End Sub

    Private Sub btnGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuardar.Click
        'Verificar valores de la tabla
        Dim i As Integer
        If cmbToma.Text = "" Then
            MsgBox("Seleccione Antes o Despues del entrenamiento.", MsgBoxStyle.Exclamation)
            Exit Sub
        End If
        For i = 0 To Grid1.Rows.Count - 1
            If IsNumeric(Grid1.Rows(i).Cells(1).Value) = False Then
                MsgBox("Todos los numeros de la columna Talla deben ser numericos", MsgBoxStyle.Exclamation)
                Exit Sub
            End If
        Next

        Dim texto As String
        Dim comando As New OleDbCommand
        comando.Connection = Conexion

        For i = 0 To Grid1.Rows.Count - 1
            If Grid1.Rows(i).Cells(1).Value <> 0 Then
                texto = "Insert into medidas (fecha, usuario, parte, medida, unidad, pres_post) values('"
                texto = texto & dtpFecha.Value.ToShortDateString & "', '"
                texto = texto & Usuario & "', '"
                texto = texto & Grid1.Rows(i).Cells(0).Value & "', '"
                texto = texto & Grid1.Rows(i).Cells(1).Value & "', '"
                texto = texto & "X" & "', '"
                texto = texto & cmbToma.Text & "')"
                comando.CommandText = texto
                comando.ExecuteNonQuery()
            End If
        Next

        MsgBox("Datos Almacenado", MsgBoxStyle.Information)
        Preparartabla()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Preparartabla()

    End Sub
End Class